// axios
import axios from '@/config/axios/index.ts';
// 模态框和打印组件
import { VxeUI } from '@/config/vxe/index.ts';
import { reactive, ref } from 'vue';
import type { JsonResultProps } from '#/JsonResult.d.ts';
import { FormInst, FormProps, FormRules, FormValidationError } from 'naive-ui';
import type {
	DictTypeProps, DictValueProps
} from '#/src/components/page/systemManager/dictMaintain/DictionaryMaintain.d.ts';
const useLeftEditModalHooks = (props: any, SlotModal: any) => {
	// ----------------------------------------------------------------------
	const slotModalConfig = reactive({
		reLoad            : () => props.reLoad(),
		onOpenModalEvent  : () => onOpenModalEvent(),
		onCloseModalEvent : () => clearFormData(),
		resetEvent        : () => clearFormData()
	});
	const formConfig = reactive<FormProps>({
		labelWidth           : 140,
		labelAlign           : 'left',
		labelPlacement       : 'left',
		size                 : 'large',
		requireMarkPlacement : 'right-hanging'
	});
	// ----------------------------------------------------------------------
	const slotModalRef = ref<InstanceType<typeof SlotModal>>();
	const formRef = ref<FormInst>();
	// ----------------------------------------------------------------------
	const currentData = reactive<Partial<DictValueProps>>({});
	const formData = reactive<Partial<DictValueProps>>({});
	const formRules = reactive<FormRules>({
		sort               : {
			required : true,
			type     : 'number',
			trigger  : ['input', 'blur'],
			message  : '序号是必须的'
		},
		dictTypeCode       : {
			required : true,
			type     : 'string',
			trigger  : ['input', 'blur'],
			message  : '字典类型编码是必须的'
		},
		dictTypeName       : {
			required : true,
			type     : 'string',
			trigger  : ['input', 'blur'],
			message  : '字典类型名称是必须的'
		},
		dictDataCode       : {
			required : true,
			type     : 'string',
			trigger  : ['input', 'blur'],
			message  : '字典值编码是必须的'
		},
		parentDictDataCode : {
			required : true,
			type     : 'string',
			trigger  : ['input', 'blur'],
			message  : '上级字典值编码是必须的'
		},
		label              : {
			required : true,
			type     : 'string',
			trigger  : ['input', 'blur'],
			message  : '字典值名称是必须的'
		},
		value              : {
			required : true,
			type     : 'string',
			trigger  : ['input', 'blur'],
			message  : '字典值是必须的'
		},
		/*disabled     : {
		 required : true,
		 type     : 'string',
		 trigger  : ['change'],
		 message  : '是否不可选是必须的'
		 },*/
		/*dictType     : {
		 required : true,
		 type     : 'string',
		 trigger  : ['change'],
		 message  : '字典值类型是必须的'
		 },*/
		status : {
			required : true,
			type     : 'boolean',
			trigger  : ['input', 'blur'],
			message  : '字典启用状态是必选的'
		}
	});
	// ----------------------------------------------------------------------
	const showModal = async (rowData: DictTypeProps) => {
		Object.assign(currentData, rowData);
		await slotModalRef.value!.openModal({
			height     : '400',
			width      : '700',
			title      : '修改字典类型',
			showClose  : true,
			showZoom   : true,
			fullscreen : false
		});
		// await initDict();
	};
	const onOpenModalEvent = async (): Promise<void> => {
		console.log(currentData);
		return new Promise((resolve, reject): void => {
			axios<any, JsonResultProps<any>, any>({
				method : 'GET',
				url    : `/dict/value/detail/${currentData.dictTypeCode}/${currentData.dictDataCode}`
			}).then((jsonResult: JsonResultProps<any>) => {
				if (jsonResult.status) {
					const data = jsonResult.data;
					Object.assign(formData, {
						dictTypeCode       : data.dictTypeCode,
						dictDataCode       : data.dictDataCode,
						parentDictDataCode : data.parentDictDataCode,
						dictType           : data.dictType,
						label              : data.label,
						value              : data.value,
						disabled           : data.disabled,
						status             : data.status,
						sort               : data.sort
					});
					resolve();
				} else {
					VxeUI.modal.message({
						content : jsonResult['msg'],
						status  : 'warning',
						id      : 'failedMessage'
					});
					reject();
				}
			}).catch((error: unknown) => {
				console.error(error);
				VxeUI.modal.message({
					content : '系统异常,请稍后再试',
					status  : 'error',
					id      : 'errorMessage'
				});
				reject();
			});
		});
	};
	const clearFormData = async () => {
		// 清理表单数据
		Object.assign(formData, {
			dictTypeCode       : null,
			dictDataCode       : null,
			parentDictDataCode : null,
			dictType           : null,
			label              : null,
			value              : null,
			disabled           : false,
			status             : true,
			sort               : null
		});
		// 去除表单校验提示
		formRef.value?.restoreValidation();
	};
	const submitEvent = async () => {
		// 校验菜单
		formRef.value?.validate((errors?: Array<FormValidationError>): void => {
			if (errors) {
				VxeUI.modal.message({ content : '请先完善填写的内容!', status : 'warning', id : 'validateMessage' });
			} else {
				// 提交数据
				axios<any, JsonResultProps<any>, any>({
					method : 'PUT',
					url    : '/dict/value/modify',
					data   : formData
				}).then((jsonResult: JsonResultProps<any>) => {
					if (jsonResult['status']) {
						// 提示用户
						VxeUI.modal.message({
							content : jsonResult['msg'],
							status  : 'success',
							id      : 'successMessage'
						});
						// 刷新数据
						props.reLoad();
						slotModalRef.value!.closeModal();
					} else {
						VxeUI.modal.message({
							content : jsonResult['msg'],
							status  : 'warning',
							id      : 'failedMessage'
						});
					}
				}).catch((error: unknown) => {
					console.error(error);
					VxeUI.modal.message({
						content : '系统异常,请稍后再试',
						status  : 'error',
						id      : 'errorMessage'
					});
				});
			}
		});
	};
	// -----------------------------------------------------------------------------------
	return {
		slotModalConfig, formConfig,
		slotModalRef, formRef,
		formData, formRules,
		showModal,
		clearFormData, submitEvent
	};
};
export default useLeftEditModalHooks;